/**
 * Copyright (c) 2010 by Kevin Bierhoff.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *    3. Neither the names of the authors nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.syper.javasyp.formulae;

import java.util.ArrayList;
import java.util.List;

/**
 * This is a base class for rewriting visitors that itself produces
 * a deep copy of the formula it works on.
 * @author kevin
 * @since Jan 30, 2010
 */
public class RewritingVisitor extends FormulaVisitor<Formula> {

	@Override
	public Formula visit(Local f) {
		return f;
	}

	@Override
	public Formula visit(Field f) {
		Field result = new Field(
				(Name) f.getObj().dispatch(this),
				(Local) f.getSelector().dispatch(this),
				f.getIncarnation());
		result.setBinding(f.getBinding());
		return result;
	}

	@Override
	public Formula visit(Infix f) {
		return new Infix(f.getLhs().dispatch(this), f.getOp(), f.getRhs().dispatch(this));
	}

	@Override
	public Formula visit(Literal f) {
		return f;
	}

	@Override
	public Formula visit(Prefix f) {
		return new Prefix(f.getOp(), f.getRhs().dispatch(this));
	}

	@Override
	public Formula visit(Select f) {
		return new Select(f.getArray().dispatch(this), f.getIndex().dispatch(this), f.getType(), f.getIncarnation());
	}

	@Override
	public Formula visit(Store f) {
		return new Store(f.getArray().dispatch(this),
				f.getIndex().dispatch(this),
				f.getNewValue().dispatch(this),
				f.getElementType());
	}

	@Override
	public Formula visit(Pre f) {
		return new Pre((Name) f.getAged().dispatch(this), f.getArrayDim());
	}

	@Override
	public Formula visit(Quantifier f) {
		return new Quantifier(f.isExists(), f.getVars(), f.getBody().dispatch(this));
	}

	@Override
	public Formula visit(Fun f) {
		List<Formula> args = new ArrayList<Formula>(f.getArgs().size());
		for (Formula a : f.getArgs())
			args.add(a.dispatch(this));
		return new Fun(f.getId(), args, f.getType());
	}

	@Override
	public Formula visit(IfThenElse f) {
		Formula cond = f.getCondition().dispatch(this);
		Formula thenF = f.getThenFormula().dispatch(this);
		Formula elseF = f.getElseFormula().dispatch(this);
		return new IfThenElse(cond, thenF, elseF);
	}
}
